﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using Abp.UI;
using CPPProject.CPP.Dto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CPPProject.CPP
{
    /// <summary>
    /// 资产档案管理
    /// </summary>
    public class CPPPartAppService : AsyncCrudAppService<CPPPart, CPPPartDto, Guid, PagedResultRequestDto, CreateCPPPartDto, UpdateCPPPartDto>
    {
        private readonly IObjectMapper _objectMapper;
        public CPPPartAppService(
            IRepository<CPPPart, Guid> repository,
            IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }
        /// <summary>
        /// 分页gitlist
        /// </summary>
        /// <param name="skipCount"></param>
        /// <param name="maxResultCount"></param>
        /// <param name="name">资产名称</param>
        /// <param name="PartNum">资产编码</param>
        /// <returns></returns>
        public async Task<PagedResultDto<CPPPartDto>> GetList(int skipCount = 0, int maxResultCount = 10, string name="", string partNum = "")
        {
            PagedResultDto<CPPPartDto> resultDto = new PagedResultDto<CPPPartDto>();
            List<CPPPart> lists = new List<CPPPart>();
            Expression<Func<CPPPart, bool>> expr = null;
            if (!string.IsNullOrEmpty(name))
            {
                expr = expr == null ? o => o.Name.Contains(name) : expr.ExpressionAnd(o => o.Name.Contains(name));
            }
            if (!string.IsNullOrEmpty(partNum))
            {
                expr = expr == null ? o => o.PartNum.Contains(partNum) : expr.ExpressionAnd(o => o.PartNum.Contains(partNum));
            }
            if (expr != null)
            {
                lists = await Repository.GetAllListAsync(expr);
                lists = lists.OrderBy(o => o.PartNum).ToList();
                var dtos = lists.Skip(skipCount).Take(maxResultCount).ToList();
                resultDto.Items = _objectMapper.Map<List<CPPPartDto>>(dtos);
                resultDto.TotalCount = lists.Count;
            }
            else
                resultDto = await base.GetAllAsync(new PagedResultRequestDto { MaxResultCount = maxResultCount, SkipCount = skipCount });
            return resultDto;
        }
        
        /// <summary>
        /// 重写Create方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<CPPPartDto> CreateAsync(CreateCPPPartDto input)
        {
            var lists = await Repository.GetAll().Where(o => o.PartNum == input.PartNum || o.Name == input.Name).ToListAsync();
            if (lists.Count <= 0)
            {
                return await base.CreateAsync(input);
            }
            else
            {
                throw new UserFriendlyException(input.Name + "已经存在");
            }
        }



        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">guid数组</param>
        /// <returns></returns>
        public async Task BatchDelete(Guid[] ids)
        {
            await Repository.DeleteAsync(o => ids.Contains(o.Id));
        }


    }
}
